/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "MWmi_Win32_Processor.h"

//---------------------------------------------------------------------------

MWmi_Win32_Processor::MWmi_Win32_Processor(void)
{
	propertyCount = 22;
	Initialize();
}

MWmi_Win32_Processor::~MWmi_Win32_Processor(void)
{
	DeleteChild();
}

//---------------------------------------------------------------------------

MWmiInfo* MWmi_Win32_Processor::CreateChild(void)
{
	MWmi_Win32_Processor *elem;

	try{
		elem = new MWmi_Win32_Processor;
	}
	catch(...){
		elem = NULL;
	}
	return elem;
}

void MWmi_Win32_Processor::DeleteChild(void)
{
	MWmi_Win32_Processor *elem;
	
	if(next!=NULL){
		elem = (MWmi_Win32_Processor*)next;
		next = NULL;
		delete elem;
	}
}

//---------------------------------------------------------------------------

void MWmi_Win32_Processor::Initialize(void)
{
	xAddressWidth           = 0;
	xArchitecture           = 0;
	xCpuStatus              = 0;
	xCurrentClockSpeed      = 0;
	xCurrentVoltage         = 0;
	xDataWidth              = 0;
	xDescription            = _T("");
	xDeviceID               = _T("");
	xExtClock               = 0;
	xFamily                 = 0;
	xLevel                  = 0;
	xLoadPercentage         = 0;
	xManufacturer           = _T("");
	xMaxClockSpeed          = 0;
	xName                   = _T("");
	xOtherFamilyDescription = _T("");
	xProcessorId            = _T("");
	xProcessorType          = 0;
	xRevision               = 0;
	xStepping               = _T("");
	xVersion                = _T("");
	xVoltageCaps            = 0;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_Processor::GetQueryString(void)
{
	CString str;

	str = _T("select * from Win32_Processor");
	return str;
}

//---------------------------------------------------------------------------

CString MWmi_Win32_Processor::GetPropertyName(int idxIn)
{
	CString str;

	switch(idxIn){
		case 0 :
			str = _T("AddressWidth");
			break;
		case 1 :
			str = _T("Architecture");
			break;
		case 2 :
			str = _T("CpuStatus");
			break;
		case 3 :
			str = _T("CurrentClockSpeed");
			break;
		case 4 :
			str = _T("CurrentVoltage");
			break;
		case 5 :
			str = _T("DataWidth");
			break;
		case 6 :
			str = _T("Description");
			break;
		case 7 :
			str = _T("DeviceID");
			break;
		case 8 :
			str = _T("ExtClock");
			break;
		case 9 :
			str = _T("Family");
			break;
		case 10 :
			str = _T("Level");
			break;
		case 11 :
			str = _T("LoadPercentage");
			break;
		case 12 :
			str = _T("Manufacturer");
			break;
		case 13 :
			str = _T("MaxClockSpeed");
			break;
		case 14 :
			str = _T("Name");
			break;
		case 15 :
			str = _T("OtherFamilyDescription");
			break;
		case 16 :
			str = _T("ProcessorId");
			break;
		case 17 :
			str = _T("ProcessorType");
			break;
		case 18 :
			str = _T("Revision");
			break;
		case 19 :
			str = _T("Stepping");
			break;
		case 20 :
			str = _T("Version");
			break;
		case 21 :
			str = _T("VoltageCaps");
			break;

		default :
			str = _T("");
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_Processor::SetPropertyValue(int idxIn, VARIANT *vtIn)
{
	if(vtIn==NULL) return true;
	if(vtIn->vt==VT_NULL) return true;

	switch(idxIn){
		case 0 :
			if(vtIn->vt!=VT_I4) return false;
			xAddressWidth = (unsigned short)vtIn->lVal;
			break;
		case 1 :
			if(vtIn->vt!=VT_I4) return false;
			xArchitecture = (unsigned short)vtIn->lVal;
			break;
		case 2 :
			if(vtIn->vt!=VT_I4) return false;
			xCpuStatus = (unsigned short)vtIn->lVal;
			break;
		case 3 :
			if(vtIn->vt!=VT_I4) return false;
			xCurrentClockSpeed = vtIn->lVal;
			break;
		case 4 :
			if(vtIn->vt!=VT_I4) return false;
			xCurrentVoltage = (unsigned short)vtIn->lVal;
			break;
		case 5 :
			if(vtIn->vt!=VT_I4) return false;
			xDataWidth = (unsigned short)vtIn->lVal;
			break;
		case 6 :
			if(vtIn->vt!=VT_BSTR) return false;
			xDescription = vtIn->bstrVal;
			break;
		case 7 :
			if(vtIn->vt!=VT_BSTR) return false;
			xDeviceID = vtIn->bstrVal;
			break;
		case 8 :
			if(vtIn->vt!=VT_I4) return false;
			xExtClock = vtIn->lVal;
			break;
		case 9 :
			if(vtIn->vt!=VT_I4) return false;
			xFamily = (unsigned short)vtIn->lVal;
			break;
		case 10 :
			if(vtIn->vt!=VT_I4) return false;
			xLevel = (unsigned short)vtIn->lVal;
			break;
		case 11 :
			if(vtIn->vt!=VT_I4) return false;
			xLoadPercentage = (unsigned short)vtIn->lVal;
			break;
		case 12 :
			if(vtIn->vt!=VT_BSTR) return false;
			xManufacturer = vtIn->bstrVal;
			break;
		case 13 :
			if(vtIn->vt!=VT_I4) return false;
			xMaxClockSpeed = vtIn->lVal;
			break;
		case 14 :
			if(vtIn->vt!=VT_BSTR) return false;
			xName = vtIn->bstrVal;
			break;
		case 15 :
			if(vtIn->vt!=VT_BSTR) return false;
			xOtherFamilyDescription = vtIn->bstrVal;
			break;
		case 16 :
			if(vtIn->vt!=VT_BSTR) return false;
			xProcessorId = vtIn->bstrVal;
			break;
		case 17 :
			if(vtIn->vt!=VT_I4) return false;
			xProcessorType = (unsigned short)vtIn->lVal;
			break;
		case 18 :
			if(vtIn->vt!=VT_I4) return false;
			xRevision = (unsigned short)vtIn->lVal;
			break;
		case 19 :
			if(vtIn->vt!=VT_BSTR) return false;
			xStepping = vtIn->bstrVal;
			break;
		case 20 :
			if(vtIn->vt!=VT_BSTR) return false;
			xVersion = vtIn->bstrVal;
			break;
		case 21 :
			if(vtIn->vt!=VT_I4) return false;
			xVoltageCaps = vtIn->lVal;
			break;

		default :
			return false;
	}

	return true;
}

//---------------------------------------------------------------------------

bool MWmi_Win32_Processor::SetFromPointer(MWmiInfo* wmiInfoIn)
{
	MWmi_Win32_Processor *elem;
	MWmi_Win32_Processor *elemNew;

	DeleteChild();
	Initialize();
	if(wmiInfoIn==NULL) return true;

	elem = (MWmi_Win32_Processor*)wmiInfoIn;

	xAddressWidth           = elem->xAddressWidth;
	xArchitecture           = elem->xArchitecture;
	xCpuStatus              = elem->xCpuStatus;
	xCurrentClockSpeed      = elem->xCurrentClockSpeed;
	xCurrentVoltage         = elem->xCurrentVoltage;
	xDataWidth              = elem->xDataWidth;
	xDescription            = elem->xDescription;
	xDeviceID               = elem->xDeviceID;
	xExtClock               = elem->xExtClock;
	xFamily                 = elem->xFamily;
	xLevel                  = elem->xLevel;
	xLoadPercentage         = elem->xLoadPercentage;
	xManufacturer           = elem->xManufacturer;
	xMaxClockSpeed          = elem->xMaxClockSpeed;
	xName                   = elem->xName;
	xOtherFamilyDescription = elem->xOtherFamilyDescription;
	xProcessorId            = elem->xProcessorId;
	xProcessorType          = elem->xProcessorType;
	xRevision               = elem->xRevision;
	xStepping               = elem->xStepping;
	xVersion                = elem->xVersion;
	xVoltageCaps            = elem->xVoltageCaps;

	if(elem->next==NULL) return true;

	try{
		elemNew = new MWmi_Win32_Processor;
	}
	catch(...){
		elemNew = NULL;
	}
	if(elemNew==NULL) return false;

	next = elemNew;

	return elemNew->SetFromPointer(elem->next);
}

//---------------------------------------------------------------------------

bool MWmi_Win32_Processor::IsEmpty(void)
{
	return xName.IsEmpty() ? true : false;
}

//---------------------------------------------------------------------------
